/**************************************************************************************
 *  Copyright (c)  2010  Leszek Karcz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package view;
import java.awt.*;

import javax.swing.*;

import data.*;

/**************************************************************************************
 * Author(s): Leszek Karcz
 * This class displays all the elements of viewable area: terrain/units/buildings/etc.
 **************************************************************************************/
public class AreaView extends JPanel
{
	private static final long serialVersionUID = -1759594161538852951L;
	private GameplayPlayerData gameplayPlayerData;
	private Images images;
	private int w,h,x,y;

	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * AreaView constructor
	 **************************************************************************************/
	public AreaView(GameplayPlayerData gameplayPlayerData, Images images)
	{
		this.gameplayPlayerData=gameplayPlayerData;
		this.images=images;
		this.setFocusable(false);
		this.setOpaque(false);
		
		this.setLocation(0, 0);
		w = 815;
		h = 530;
		x=5;
		y=30;
		
		this.setSize(w, h);
		this.setLocation(x, y);
	
	}

	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * paints/repaints the viewport
	 **************************************************************************************/
	public void paint(Graphics g)
	{
		this.setSize(w, h);
		this.setLocation(x, y);
		
		//Draw visible elements one tile at a time
		for(int i=0; i<9; i++)
		{
			for(int j=-1; j<7; j++)
			{
				int posx,posy;
				posx=i*100-33;
				posy=j*116;
				
				int mod=(this.gameplayPlayerData.getCursorPosition()[0] % 2);
				//adjust posy depending on i and cursorX being odd/even
				if((i % 2) != 0)
				{
					posy+=(mod==0 ? 58 : -58);
				}
							
				//check visibility status
				boolean[] visibility=this.gameplayPlayerData.getVisibilityStatus(i,j,true);
				if(visibility[0]) //discovered
				{
					//get and draw terrain
					String[] terrain = this.gameplayPlayerData.getTileTerrainData(i,j,true);
					for(int t=0;t<terrain.length;t++)
					{
					g.drawImage(images.getImage(terrain[t]), posx, posy, this);
					}
					
					//get and draw top item
					String item=this.gameplayPlayerData.getTopItemData(i, j, true);
					g.drawImage(images.getImage(item), posx, posy, this);
					
					if(visibility[1]) //resources discovered
					{
						//draw resources
						Font font = new Font("Arial", Font.BOLD, 12);
						g.setColor(Color.green);
						g.setFont(font);
						String[] resourceImages=this.gameplayPlayerData.getTileResourceImages(i,j,true);	
						String[] resourceData=this.gameplayPlayerData.getTileResourceData(i, j, true);
						for(int r=0; r<resourceImages.length; r++)
						{
							g.drawImage(images.getImage(resourceImages[r]), posx, posy, this);
						}
						
						
						g.drawString(""+resourceData[0], posx+17, posy+82);
						g.drawString(""+resourceData[1], posx+90, posy+82);
						g.drawString(""+resourceData[2], posx+55, posy+105);					
					}
					
					if(visibility[2]) //currently visible
					{
						//get and draw top controllables
						String[][] imageData=this.gameplayPlayerData.getTileImageData(i, j, true);
						
						//draw highlights
						for(int c=0;c<imageData.length;c++)
						{
							if(imageData[c]!=null)
							{								
								//draw top controllable
								g.drawImage(images.getImage(imageData[c][0]), posx, posy, this);
							}
						}
						//draw controllalbes
						for(int c=0;c<imageData.length;c++)
						{
							if(imageData[c]!=null)
							{	
								//draw top controllable
								g.drawImage(images.getImage(imageData[c][1]), posx, posy, this);
							
								if(c==2)
								{
									//draw health bar
									g.setColor(Color.BLACK);
									g.fillRect(posx+37, posy+108, 61, 4);
									g.setColor(Color.green);
									g.fillRect(posx+38, posy+109, (int)(59*Integer.parseInt(imageData[c][2])/100), 2);
								}
							
							}
						}
						
					}
					
							
				
					if(!visibility[2]) //under fog of war
					{
						g.drawImage(images.getImage("tile_fow"), posx, posy, this);
					}
				}
				else //not discovered
				{
					g.drawImage(images.getImage("tile_empty"), posx, posy, this);
				}
			}
		}
	}
}
